#!/usr/bin/env python
#coding=utf8
__author__ = 'chenglin'
from flask import render_template, flash, redirect, session, url_for, request, g
from web import app
import urllib2
import json
import hashlib



@app.route('/')
@app.route('/index')
def index():
    return render_template('index.html',
                           title=u'微信上传图片测试')

@app.route('/token',methods=['GET','POST'])
def token():
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxd01d9d2a4803b73e&secret=6a5810e8e7165687987e593240322c00'
    req = urllib2.Request(url)
    htmlH = urllib2.urlopen(req)
    respData = htmlH.read()
    respData = respData.decode('utf8')

    decodejson = json.loads(respData)
    return decodejson['access_token']

@app.route('/auth',methods=['GET','POST'])
def jsapi():
    access_token = token()
    url = 'https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=%s&type=jsapi' %  (access_token)
    print url
    req = urllib2.Request(url)
    respData = urllib2.urlopen(req).read()
    decodejson = json.loads(respData)
    return decodejson['ticket']

@app.route('/signature',methods=['GET','POST'])
def signature():
    ticket = jsapi()
    str = 'jsapi_ticket=%s&noncestr=%s&timestamp=%s&url=%s' % (ticket ,'sduhi123', '2015102512','http://www.62eshop.com/')
    print str
    print hashlib.sha1(str).hexdigest()
    return str